﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace Fetion.Core
{
    /// <summary>
    /// 对象协议封装基础类
    /// </summary>
    public abstract class Package 
    {
        /// <summary>
        /// 构建对像协议封装
        /// </summary>
        public Package()
        {
            Coding = Encoding.UTF8;
           
        }
       
        /// <summary>
        /// 或取或设置字符编码
        /// </summary>
        public Encoding Coding
        {
            get;
            set;
        }
        public TcpChannel Channel
        {
            get;
            set;
        }
        public event Action<IMessage> Receive;
        protected virtual void OnReceive(IMessage e)
        {
            if (Receive != null)
                Receive(e);
        }
        public abstract byte[] GetMessageData(IMessage e);
        /// <summary>
        /// 导入数据信息处理,重写此方法进行数据协议分析
        /// </summary>
        /// <param name="data">接收的byte数组</param>
        /// <param name="start">起始索引</param>
        /// <param name="count">长度</param>
        public abstract void Import(byte[] data, int start, int count, EventHandler<ClientAsyncCompletedEventArgs<object>> handle);
        //public abstract void Import(byte[] data, int start, int count,EventHandler handleResult);
        /// <summary>
        /// 查询一个byte数组在别一个byte数组中的位置,-1表示不存在
        /// </summary>
        /// <param name="searched">查询数据</param>
        /// <param name="find">数据源</param>
        /// <returns>int</returns>
        public static int ByteIndexOf(byte[] searched, byte[] find)
        {
            
            return ByteIndexOf(searched, find, 0);
        }
        /// <summary>
        /// 查询一个byte数组在别一个byte数组中的位置,-1表示不存在
        /// </summary>
        /// <param name="searched">查询数据</param>
        /// <param name="find">数据源</param>
        /// <param name="start">数据源起始位置查询</param>
        /// <returns>int</returns>
        public static int ByteIndexOf(byte[] searched, byte[] find, int start)
        {
            return ByteIndexOf(searched, find, searched.Length - start);
        }
        /// <summary>
        /// 查询一个byte数组在别一个byte数组中的位置,-1表示不存在
        /// </summary>
        /// <param name="searched">查询数据</param>
        /// <param name="find">数据源</param>
        /// <param name="start">数据源起始位置查询</param>
        /// <param name="count">查询总长度</param>
        /// <returns>int</returns>
        public static int ByteIndexOf(byte[] searched, byte[] find, int start, int count)
        {
            bool matched = false;
            int end = find.Length - 1;
            int skip = 0;
            int fcount = start + count - find.Length;
            for (int index = start; index <= fcount; ++index)
            {
                skip = 0;
                matched = true;
                if (find[0] != searched[index] || find[end] != searched[index + end])
                    continue;
                else
                    skip++;
             
                if (end > 10)
                    if (find[skip] != searched[index + skip] || find[end - skip] != searched[index + end - skip])
                        continue;
                    else
                        skip++;
                for (int subIndex = skip; subIndex < find.Length - skip; ++subIndex)
                {
                    if (find[subIndex] != searched[index + subIndex])
                    {
                        matched = false;
                        break;
                    }
                }
                if (matched)
                {
                    return index + find.Length-1;
                }
            }
            return -1;
        }
        /// <summary>
        /// 是否开启Variant方式写入数字，默认是false
        /// </summary>
        public bool EnabledVariant = false;

        public virtual object WriteCast(object message)
        {
            return message;
        }
        public virtual object ReadCast(object message)
        {
            return message;
        }

 
    }
 
  
}
